package com.bignerdranch.android.criminalintent;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;

public class ImageOptionsFragment extends DialogFragment {
public static final String EXTRA_OPTION = "com.bignerdranch.android.criminalintent.imageOption";
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
	    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
	    builder.setTitle(R.string.image_options)
	    .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				sendResult(Activity.RESULT_CANCELED, which);
			}
		})
	    	.setItems(R.array.imageoptions, new DialogInterface.OnClickListener() {				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					sendResult(Activity.RESULT_OK, which);
				}
			});
	    return builder.create();	
	}
	
	private void sendResult(int resultCode, int which){
		if(getTargetFragment() == null){
			return;
		}
		Intent i = new Intent();
		i.putExtra(EXTRA_OPTION, which);
		getTargetFragment().onActivityResult(getTargetRequestCode(), resultCode, i);		
	}
}
